package com.woorigil.social.parser.wemakeprice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.jsoup.Jsoup;

import com.woorigil.social.HttpUtils;
import com.woorigil.social.domain.ProviderCode;
import com.woorigil.social.parser.CategoryMapper;

public class LocalSiteParser {
	String baseUrl = "http://www.wemakeprice.com/main/get_deal_more/${}?_=1374058303003&curr_deal_cnt=0&r_cnt=5000";
	
	static List<String> localUrls = new ArrayList<String>() {{
		add("1100");
		add("4100");
		add("2800");
		add("2610");
		add("2710");
		add("2910");
		add("3010");
		add("8000");
	}};
	
	public Map<String, WemakeExtraDeal> parse() {
		Map<String, WemakeExtraDeal> extraDeals = new HashMap<String, WemakeExtraDeal>();
		parseForFood(extraDeals);
		parseForButi1(extraDeals);
		parseForButi2(extraDeals);
		return extraDeals;
	}
	
	public Map<String, WemakeExtraDeal> parseForFood(Map<String, WemakeExtraDeal> extraDeals) {
		for (String each : localUrls) {
			try {
				String data = HttpUtils.execute(baseUrl.replace("${}", each) + "&filter=1");
				String html = new JSONObject(data).getString("html");
				org.jsoup.nodes.Document doc = Jsoup.parse(html);
				org.jsoup.select.Elements td1s = doc.select("li");
				
				for (org.jsoup.nodes.Element element : td1s) {
					WemakeExtraDeal extraDeal = extractDealInfo(element, CategoryMapper.DEPTH_ONE_CATEGORY_FOOD, td1s);
					if ("114477".equals(extraDeal.getMetaId())) {
						System.out.println("맛집 : " + extraDeal.getMetaId());
					}
					extraDeals.put(extraDeal.getMetaId(), extraDeal);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return extraDeals;
	}
	
	public Map<String, WemakeExtraDeal> parseForButi1(Map<String, WemakeExtraDeal> extraDeals) {
		for (String each : localUrls) {
			try {
				String data = HttpUtils.execute(baseUrl.replace("${}", each) + "&filter=2");
				String html = new JSONObject(data).getString("html");
				org.jsoup.nodes.Document doc = Jsoup.parse(html);
				org.jsoup.select.Elements td1s = doc.select("li");
				
				for (org.jsoup.nodes.Element element : td1s) {
					WemakeExtraDeal extraDeal = extractDealInfo(element, CategoryMapper.DEPTH_ONE_CATEGORY_BUTI, td1s);
					if ("114477".equals(extraDeal.getMetaId())) {
						System.out.println("뷰티 : " + extraDeal.getMetaId());
					}
					extraDeals.put(extraDeal.getMetaId(), extraDeal);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return extraDeals;
	}
	
	public Map<String, WemakeExtraDeal> parseForButi2(Map<String, WemakeExtraDeal> extraDeals) {
		for (String each : localUrls) {
			try {
				String data = HttpUtils.execute(baseUrl.replace("${}", each) + "&filter=4");
				String html = new JSONObject(data).getString("html");
				org.jsoup.nodes.Document doc = Jsoup.parse(html);
				org.jsoup.select.Elements td1s = doc.select("li");
				
				for (org.jsoup.nodes.Element element : td1s) {
					WemakeExtraDeal extraDeal = extractDealInfo(element, CategoryMapper.DEPTH_ONE_CATEGORY_BUTI, td1s);
					if ("114477".equals(extraDeal.getMetaId())) {
						System.out.println("생활 : " + extraDeal.getMetaId());
					}
					extraDeals.put(extraDeal.getMetaId(), extraDeal);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return extraDeals;
	}
	

	private WemakeExtraDeal extractDealInfo(org.jsoup.nodes.Element element, int mainCategoryId, org.jsoup.select.Elements td1s) {
		String metaId = element.attr("deal_id");
		String imageUrl = element.select(".box_thumb img").attr("original");
		WemakeExtraDeal extraDeal = new WemakeExtraDeal(ProviderCode.WM, metaId, imageUrl, 0);
		extraDeal.setMainCategoryId(mainCategoryId);
		return extraDeal;
	}	
}
